﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using BOL;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;




namespace UILGestion.ObraAdmin.RendicionCaja
{
    public partial class RendicionAddPanel : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {


                if (!this.IsPostBack)
                {
                    // inicializamos el esquema
                    //idParent.Value = (string)Request.QueryString["idProy"];

                    FormatForm();
                }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }
        private void FormatForm()
        {
             ShowComboBox();

            
        }
        public void ShowComboBox()
        {


            var q = new ListValueBL().GetFormaEntrega();
            tipoEntregaDDL.DataTextField = "descripcion";
            tipoEntregaDDL.DataValueField = "idInternal";
            tipoEntregaDDL.DataSource = q.ToList();
            tipoEntregaDDL.DataBind();
        }
        public void Load(Caja caja)
        {
            Rendicion rendicion = new Rendicion();
            idObra.Value = caja.idObra.ToString();
            idCaja.Value = caja.idInternal.ToString();

           short itemUltimo= rendicion.GetUltimoItemRendicion(DbUtil.Db,idCaja.Value.ToInt());

            decimal totalSaldo=0;
            var rencionUltimo=caja.Rendicions.SingleOrDefault(p => p.itemRendicion == itemUltimo);
            if (rencionUltimo != null)
            {

                totalSaldo = rencionUltimo.totalSaldo;
            }   

            NumeroRendicionTxt.Text=rendicion.getCorrelativo(DbUtil.Db,caja.idObra);
            descripcionTextBox.Text=caja.descripcion;
            cajaLabel.Text=caja.descripcion;
            simbolLBL.Text = caja.Moneda.simbol;
            simbol1LBL.Text = caja.Moneda.simbol;
            simbol2LBL.Text = caja.Moneda.simbol;
            nroDepositoTXT.Text=string.Empty;
            fechaTXT.Text=DateTime.Now.ToShortDateString();
            saldoAnteriorLBL.Text =totalSaldo.ToString();
            
            
            
            monedaLBL.Text = caja.Moneda.descripcion;
            simbol3LBL.Text = caja.Moneda.simbol;
            totalRefLBL.Text = caja.montoAsignado.ToString("N");

            //------------------------------------------------------------


            totalDepositoLBL.Text = (caja.montoAsignado - (totalSaldo)).ToString();

            totalTXT.Text = (caja.montoAsignado - (totalSaldo)).ToString();
            
            //-------------------------------------------------
            totalDepositoLBL.Text = (totalTXT.Text.ToDecimal().Value).ToString();
            totalFinalLBL.Text = (saldoAnteriorLBL.Text.ToDecimal().Value + totalDepositoLBL.Text.ToDecimal().Value).ToString();
        }

        protected void AddRendicion(object sender, EventArgs e)
        {
            try
            {


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
           
        }


        

        public RendicionDepositoInfo depositoInfo
        {
            get
            {
                var q = new RendicionDepositoInfo();
                q.setAdd(
                    0,
                    fechaTXT.Text.ToDate().Value,
                    tipoEntregaDDL.SelectedValue.ToInt(),
                    nroDepositoTXT.Text,
                    totalTXT.Text.ToDecimal().Value);

                return q;
            }
        }

        public RendicionInfo rendicionInfo
        {
            get
            {
                var q = new RendicionInfo();
                q.SetAdd(idCaja.Value.ToInt(),
                           descripcionTextBox.Text,
                           idObra.Value.ToInt(),
                           39, //apertura
                           NumeroRendicionTxt.Text);
                    
                return q;
            }
           
        }

        protected void calcularIMG_Click(object sender, EventArgs e)
        {
            try
            {
                totalDepositoLBL.Text = ( totalTXT.Text.ToDecimal().Value).ToString();
                totalFinalLBL.Text = (saldoAnteriorLBL.Text.ToDecimal().Value + totalDepositoLBL.Text.ToDecimal().Value).ToString();
             }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }
    
    }
        
    
}